package jp.gr.java_conf.ryz.summarize.client.presenter;

import jp.gr.java_conf.ryz.summarize.shared.model.Post;

public class PostDecorationUtil
{
	public static String getTitleHtml(Post post, PostDecoration decoration)
	{
		return getNumberHtml(post) + getNameHtml(post) + getMailHtml(post) + getDateHeml(post);
	}
	
	public static String getBodyHtml(Post post, PostDecoration decoration)
	{
		return getBodyHtmlInternal(post, decoration);
	}
	
	
	private static String getNumberHtml(Post post)
	{
		return post.getNumber() + " : ";
	}
	
	private static String getNameHtml(Post post)
	{
		return "<span style=\"color: green; font-weight: 700\">" + post.getName() + "</span>";
	}
	
	private static String getDateHeml(Post post)
	{
		return post.getDate();
	}
	
	private static String getMailHtml(Post post)
	{
		return "<span style=\"color: gray\">[" + post.getMail() + "]</span>";
	}
	
	private static String getBodyHtmlInternal(Post post, PostDecoration decoration)
	{
		String fontSizeStyle = decoration.getFontSizeStyle();
		String style = "";
		
		if (fontSizeStyle != null)
		{
			if (style.isEmpty())
			{
				style = "style=\"";
			}
			style += fontSizeStyle;
		}
		
		String fontWeightStyle = decoration.getFontWeightStyle();
		if (fontWeightStyle != null)
		{
			if (style.isEmpty())
			{
				style = "style=\"";
			}
			style += fontWeightStyle;
		}
		
		String colorStyle = decoration.getColorStyle();
		if (colorStyle != null)
		{
			if (style.isEmpty())
			{
				style = "style=\"";
			}
			style += colorStyle;
		}
		
		if (!style.isEmpty())
		{
			style += "\"";
		}
		
		String body = post.getBody();
		body = appendAnchorStyle(body);
		
		return "<div " + style + ">" + body + "</div>";
	}
	
	private static String appendAnchorStyle(String body)
	{
		StringBuilder work = new StringBuilder(body);
		StringBuilder result = new StringBuilder();
		
		String anchorSign = "&gt;&gt;";
		int anchorLength = anchorSign.length();
		while (work.length() > 0)
		{
			int anchorIndex = work.indexOf(anchorSign);
			if (anchorIndex < 0)
			{
				result.append(work.toString());
				break;
			}
			else
			{
				// Sample
				// " &gt;&gt;1 <br> 乙。 "
				// anchorIndex=1, anchorLength=8
				
				if (work.length() >= anchorIndex - 1 + anchorLength + 4)
				{
					String anchor = work.substring(anchorIndex, anchorIndex - 1 + anchorLength + 4);
					String anchorNumber = anchor.replaceFirst(anchorSign, "");
					try
					{
						Integer.parseInt(anchorNumber);
						
						result.append(work.substring(0, anchorIndex));
						result.append("<span style=\"color:mediumblue;\">" + anchor + "</span>");
						work.delete(0, anchorIndex - 1 + anchorIndex + anchorLength + 4);
						
						continue;
					}
					catch (NumberFormatException e)
					{
						// do nothing.
					}
				}
				
				if (work.length() >= anchorIndex - 1 + anchorLength + 3)
				{
					String anchor = work.substring(anchorIndex, anchorIndex - 1 + anchorLength + 3);
					String anchorNumber = anchor.replaceFirst(anchorSign, "");
					try
					{
						Integer.parseInt(anchorNumber);
						
						result.append(work.substring(0, anchorIndex));
						result.append("<span style=\"color:mediumblue;\">" + anchor + "</span>");
						work.delete(0, anchorIndex - 1 + anchorIndex + anchorLength + 3);
						
						continue;
					}
					catch (NumberFormatException e)
					{
						// do nothing.
					}
				}
				
				if (work.length() >= anchorIndex - 1 + anchorLength + 2)
				{
					String anchor = work.substring(anchorIndex, anchorIndex - 1 + anchorLength + 2);
					String anchorNumber = anchor.replaceFirst(anchorSign, "");
					try
					{
						Integer.parseInt(anchorNumber);
						
						result.append(work.substring(0, anchorIndex));
						result.append("<span style=\"color:mediumblue;\">" + anchor + "</span>");
						work.delete(0, anchorIndex - 1 + anchorIndex + anchorLength + 2);
						
						continue;
					}
					catch (NumberFormatException e)
					{
						result.append(work.substring(0, anchorIndex + anchorLength));
						work.delete(0, anchorIndex + anchorLength);
					}
				}
				else
				{
					// ending with anchor
					// " &gt;&gt;"
					result.append(work.substring(0, anchorIndex + anchorLength));
					work.delete(0, anchorIndex + anchorLength);
				}
			}
		}
		
		return result.toString();
	}
}
